//赢家检测

module winner_detector(
            input [1:0] pos1,pos2,pos3,pos4,pos5,pos6,pos7,pos8,pos9, 

            output wire winner, 
            output wire [1:0]who

);

    wire win1,win2,win3,win4,win5,win6,win7,win8;
    wire player1,player2,player3,player4,player5,player6,player7,player8;
    wire pc1,pc2,pc3,pc4,pc5,pc6,pc7,pc8;
    wire [1:0] who1,who2,who3,who4,who5,who6,who7,who8;

    // assign win1 = ((pos1==pos2==pos3)&&(pos1!=2'd0))?1'b1:1'b0;//如果第一行全部相等，则说明赢（第一种情况）
    // assign win2 = ((pos4==pos5==pos6)&&(pos4!=0))?1'b1:1'b0;
    // assign win3 = ((pos7==pos8==pos9)&&(pos7!=0))?1'b1:1'b0;
    // assign win4 = ((pos1==pos4==pos7)&&(pos1!=0))?1'b1:1'b0;
    // assign win5 = ((pos2==pos5==pos8)&&(pos2!=0))?1'b1:1'b0;
    // assign win6 = ((pos3==pos6==pos9)&&(pos3!=0))?1'b1:1'b0;
    // assign win7 = ((pos1==pos5==pos9)&&(pos1!=0))?1'b1:1'b0;
    // assign win8 = ((pos3==pos5==pos7)&&(pos3!=0))?1'b1:1'b0;

    assign player1 =(pos1==pos2==pos3==2'b01)?1'b1:1'b0;
    assign player2 =(pos4==pos5==pos6==2'b01)?1'b1:1'b0;
    assign player3 =(pos7==pos8==pos9==2'b01)?1'b1:1'b0;
    assign player4 =(pos1==pos4==pos7==2'b01)?1'b1:1'b0;
    assign player5 =(pos2==pos5==pos8==2'b01)?1'b1:1'b0;
    assign player6 =(pos3==pos6==pos9==2'b01)?1'b1:1'b0;
    assign player7 =(pos1==pos5==pos9==2'b01)?1'b1:1'b0;
    assign player8 =(pos3==pos5==pos7==2'b01)?1'b1:1'b0;

    assign pc1 =(pos1==pos2==pos3==2'b10)?1'b1:1'b0;
    assign pc2 =(pos4==pos5==pos6==2'b10)?1'b1:1'b0;
    assign pc3 =(pos7==pos8==pos9==2'b10)?1'b1:1'b0;
    assign pc4 =(pos1==pos4==pos7==2'b10)?1'b1:1'b0;
    assign pc5 =(pos2==pos5==pos8==2'b10)?1'b1:1'b0;
    assign pc6 =(pos3==pos6==pos9==2'b10)?1'b1:1'b0;
    assign pc7 =(pos1==pos5==pos9==2'b10)?1'b1:1'b0;
    assign pc8 =(pos3==pos5==pos7==2'b10)?1'b1:1'b0;

    assign win1 = (player1 | pc1);
    assign win2 = (player2 | pc2);
    assign win3 = (player3 | pc3);
    assign win4 = (player4 | pc4);
    assign win5 = (player5 | pc5);
    assign win6 = (player6 | pc6);
    assign win7 = (player7 | pc7);
    assign win8 = (player8 | pc8);

    assign who1 = ((pos1&pos2)&pos3);//01为玩家胜利，10为电脑胜利，00为这种情况没胜利
    assign who2 = ((pos4&pos5)&pos6);
    assign who3 = ((pos7&pos8)&pos9);
    assign who4 = ((pos1&pos4)&pos7);
    assign who5 = ((pos2&pos5)&pos8);
    assign who6 = ((pos3&pos6)&pos9);
    assign who7 = ((pos1&pos5)&pos9);
    assign who8 = ((pos3&pos5)&pos7);

    assign winner = (((((((win1 | win2) | win3) | win4) | win5) | win6) | win7) | win8);//为1时，有赢者
    assign who = (((((((who1 | who2) | who3) | who4) | who5) | who6) | who7) | who8);
endmodule 